local RandomLogic = require("common.RandomLogic")
local Lang = require("common.Lang")
local ExcelMonsterConf= require("excel.Monster")
local Msg = require("core.Msg")
local ObjItem = require("core.ObjItem")
local ObjMonster = require("core.ObjMonster")
local ObjHuman = require("core.ObjHuman")
local Obj = require("core.Obj")
local Map = require("core.Map")
local math = _G.math
--local Util = require("common.Util")
local Broadcast = require("broadcast.Broadcast")
local ItemDefine = require("bag.ItemDefine")
local BagLogic = require("bag.BagLogic")
local Grid = require("bag.Grid")
local ObjCollect = require("core.ObjCollect")
local WorldDropConf = require("excel.WorldDrop")
local Copy = require("copy.CopyL")
local RoleFFI = require("role.RoleFFI")
local CopyCallback = require("copy.CallbackL")
local MonthCardLogic = require("vip.MonthCardLogic")
local Pata = require("copy.Pata")
local SceneEvent = require("core.SceneEvent")
local CopyDefine = require("copy.Define")
local WorldBossLogicL = require("worldboss.WorldBossLogicL")
local WildBossLogicL = require("wildboss.WildBossLogicL")
local HtmlUtil = require("common.HtmlUtil")
local ExcelMonster = require("excel.Monster")
local PfLogic = require("tencentApi.PfLogic")

TEAM_PICK_WEIGHT = 100                --组队拾取权重
TEAM_PICK_MIN_WEIGHT = 25             --组队拾取最低权重
TEAM_PRICK_WEIGHT_RATE = 0.5            --组队拾取权重率

-- 怪物掉落枚举
DROP_TYPE1 = 1	-- 第一刀独立掉落
DROP_TYPE2 = 2	-- 第一刀平均掉落
DROP_TYPE3 = 3	-- 所有人共享掉落
DROP_TYPE4 = 4  -- 所有攻击过的人独立掉落
DROP_TYPE5 = 5  -- 第一刀掉落，但所有人可见

-- 采集物掉落枚举
DROP_COLLECT_TYPE1 = 1 -- 采集者独享
DROP_COLLECT_TYPE2 = 2 -- 采集者和队友独立掉落
DROP_COLLECT_TYPE3 = 3 -- 采集者和队友平均掉落


PICK_UP_OK = 1										-- 拾取成功
PICK_UP_ERROR_OBJ_ID_INVALID = 2					-- 物品id非法
PICK_UP_ERROR_BAG_CAP_NOT_ENOUGH = 3				-- 背包容量不足
PICK_UP_ERROR_OBJ_ITEM_NOT_BELONG_YOU = 4			-- 物品不属于你
PICK_UP_ERROR_IN_PROTECTED_TIME = 6					-- 保护时间500

DROP_RADIUS = DROP_RADIUS or 300					--掉落半径
DROP_RADIUS_HUMAN = DROP_RADIUS_HUMAN or 100			--掉落半径
PICK_RADIUS = PICK_RADIUS or 200					--拾取半径

function pickUpItem(human, obj_id)
    if human.db.hp < 1 then
        return
    end
    local item = ObjItem.list[obj_id]
    local msgRet = Msg.gc.GC_PICK_UP_ITEM
    if not item then
        return
    end
    msgRet.obj_id = item.id
    if item.owner_uuid then
        if not (item.owner_uuid == human._id) then
            Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.PICK_UP_ERROR_OBJ_ITEM_NOT_BELONG_YOU)
            return
        end
    end
    local exYinliang = 0
    local grid = item.grid
    local pass = BagLogic.checkCanAddItem(human, grid.id, grid.bind, grid.cnt, grid.lifeTime)
    if not pass then
        if Pata.pickUpWhenBagFull(human, grid) then
            ObjItem.destroy(item)
            CopyCallback.onPickUpItemCB(human, grid.id, grid.cnt, item, grid)
            return
        end
        return Broadcast.sendBroadcast(human, Lang.MOUSE, Lang.PICK_UP_ERROR_BAG_FULL)
    end
    msgRet.obj_id = item.id
    msgRet.result = PICK_UP_OK
    msgRet.icon = ItemDefine.getIconByGrid(grid)
    Msg.send(msgRet, human.fd)
    if not item.owner_uuid then
        local sendMsg = Msg.gc.GC_BROADCAST
        sendMsg.type = Lang.CHAT
        sendMsg.msg = string.format("%s获得了  %s*%d", human.name, ItemDefine.getValue(grid.id, "name"), grid.cnt)
        Obj.sendArea(sendMsg, human.scene_id, human.x, human.y, human.fd)
    end
    local preLv, nowLv
    if ItemDefine.isEquip(grid.id) then
        BagLogic.addItemByGrid(human, nil, grid, "pickup")
    elseif ItemDefine.spobj.jingyan[grid.id] then
        preLv = human.db.lv
        ObjHuman.addExp(human, grid.cnt, nil, 1)
        nowLv = human.db.lv
    elseif ItemDefine.spobj.yinliang[item.grid.id]  then
        BagLogic.addItem(human, grid.id, grid.bind, grid.cnt+exYinliang, "pickup", nil, grid.lifeTime)
    elseif ItemDefine.spobj.bind_yuanbao[item.grid.id] then
        BagLogic.addItem(human, grid.id, grid.bind, grid.cnt, "pickup", nil, grid.lifeTime)
    else
        if ItemDefine.isItem(grid.id) then
            local cntLimit = ItemDefine.getValue(grid.id, "cntLimit")
            if cntLimit ~= 0 then
                BagLogic.addItemByGrid(human, nil, grid, "pickup")
            else
                BagLogic.addItem(human, grid.id, grid.bind, grid.cnt, "pickup", nil, grid.lifeTime)
            end
        else
            BagLogic.addItem(human, grid.id, grid.bind, grid.cnt, "pickup", nil, grid.lifeTime)
        end
    end

    if ItemDefine.spobj.jingyan[grid.id] then
        local levelUp = 0
        if preLv ~= nowLv then
            levelUp = 1
        end
        local mm = Msg.gc.GC_ITEM_ADD_EXP
        mm.objID = obj_id
        mm.levelUp = levelUp
        mm.exp = grid.cnt
        Msg.send(mm, human.fd)
    end

    if ItemDefine.spobj.yinliang[grid.id] then
        Broadcast.sendBroadcast(human, Lang.CHAT + Lang.NOT_COMPREHENSIVE, string.format(Lang.COMMON_ADD_YINLIANG, grid.cnt+exYinliang))
    end

    ObjItem.destroy(item)
    CopyCallback.onPickUpItemCB(human, grid.id, grid.cnt, item, grid)

    --发送蛋疼的拾取广播
    local nItemID = grid.id
    if ItemDefine.getValue(nItemID, "pickupDesc") == 1 then
        local nPickDescData = ItemDefine.getValue(nItemID, "pickupDescData")
        if item.dropFromMonsterType == 3 and nPickDescData[1] then
            local nWorldBoss = WorldBossLogicL.onGetBossData()
            if nWorldBoss then
                local itemName = ItemDefine.getValue(nItemID, "name")
                local nMonsterName = ExcelMonster.monster[nWorldBoss.monster_id].name
                Broadcast.sendBroadcast2World(Lang.GROSSIP + Lang.UP, string.format(nPickDescData[1], human.name, nMonsterName, HtmlUtil.fontItemId(nItemID, grid.cnt)))
            end
        elseif item.dropFromMonsterType == 6 and nPickDescData[2] then
            local nWildBoss = WildBossLogicL.onGetBossData(human.db.map_id)
            if nWildBoss then
                local itemName = ItemDefine.getValue(nItemID, "name")
                local nMonsterName = ExcelMonsterConf.monster[nWildBoss.monster_id].name
                Broadcast.sendBroadcast2World(Lang.GROSSIP + Lang.UP,  string.format(nPickDescData[2], human.name, nMonsterName, HtmlUtil.fontItemId(nItemID, grid.cnt)))
            end
        end

    end
end

local function checkTeamRate(team, teamLen)
    local onlineUuid = ObjHuman.onlineUuid
    for i=0, teamLen - 1 do
        local member = onlineUuid[RoleFFI.ffi_string(team[i].uuid)]
        if member and not member.teamPickWeight then
            member.teamPickWeight = TEAM_PICK_WEIGHT
        end
    end

end

tmpTeam = tmpTeam or {}
local function getTeam(team, monster, teamLen)
    --clear
    for k in pairs(tmpTeam) do
        tmpTeam[k] = nil
    end

    local onlineUuid = ObjHuman.onlineUuid
    for i=0, teamLen - 1 do
        local target = onlineUuid[RoleFFI.ffi_string(team[i].uuid)]
        if target and Obj.canSeeEachOther2(target, monster) then
            tmpTeam[#tmpTeam+1] = RoleFFI.ffi_string(team[i].uuid)
        end
    end

    return tmpTeam
end

function onMonsterDie(monster)
    local conf = ObjMonster.get_conf(monster)
    if not conf then
        return
    end

    local onlineUuid = ObjHuman.onlineUuid
    if conf.drop_type == DROP_TYPE1 then -- DROP_TYPE1 = 1	-- 第一刀独立掉落

        local human = ObjHuman.onlineUuid[monster.firstHitUuid]
        if not human then
            return
        end

        local team = human.db.teamUuid
        local teamLen = human.db.teamUuidLen
        if teamLen == 0 then
            -- 个人
            onMonsterDropLogic(human, monster, conf)
        else
            -- 组队
            local humanInCopy = Copy.isCopyBySceneID(human.scene_id)
            for i=0, teamLen - 1 do
                local member = onlineUuid[RoleFFI.ffi_string(team[i].uuid)]
                if member and (Obj.canSeeEachOther2(human, member) or (human.scene_id == member.scene_id and humanInCopy)) then
                    onMonsterDropLogic(member, monster, conf)
                end
            end
        end
    elseif conf.drop_type == DROP_TYPE2 then -- DROP_TYPE2 = 2	-- 第一刀平均掉落

        local human = onlineUuid[monster.firstHitUuid]
        if not human then
            return
        end
        local team = human.db.teamUuid
        local teamLen = human.db.teamUuidLen
        
        if teamLen <= 0 then
            onMonsterDropLogic(human, monster, conf)
        else
            --　check team rate
            checkTeamRate(team, teamLen)
            
            local tmpTeam = getTeam(team, monster, teamLen)
            if #tmpTeam == 0 then
                return
            end
            local itemList = onMonsterDropLogic(nil, monster, conf, true)
            local CNT = math.floor(#itemList / #tmpTeam)
            local member = nil
            local scene_id = monster.scene_id
            
            local dlen = 0
            local dtb = {}
            --平均分
            for _, m in ipairs(tmpTeam) do
                for i=1, CNT do
                    local r = math.random(1, #itemList)
                    local item = table.remove(itemList, r)
                    member = onlineUuid[m]
                    if member and checkCanDrop(member, monster, conf) then
                        item.owner_uuid = member._id
                        dlen = dlen + 1
                        dtb[dlen] = item
                    else
                        item.scene_id = nil
                        ObjItem.destroy(item)
                    end
                end
            end --end for

            local max_cnt = 0
            for _, v in ipairs(tmpTeam) do
                member = onlineUuid[v]
                if member then
                    max_cnt = max_cnt + math.ceil(member.teamPickWeight)
                end
            end

            -- 抽选
            for _, item in ipairs(itemList) do
                local destoryItem = true
                local r = math.random(1, max_cnt)
                local sum = 0
                for _, v in ipairs(tmpTeam) do
                    member = onlineUuid[v]
                    if member then
                        sum = sum + math.ceil(member.teamPickWeight)
                        if sum >= r then
                            if checkCanDrop(member, monster, conf) then
                                item.owner_uuid = member._id
                                dlen = dlen + 1
                                dtb[dlen] = item
                                destoryItem = false
                            end
                            break
                        end
                    end
                end
                if destoryItem then
                    item.scene_id = nil
                    ObjItem.destroy(item)
                end
            end --end for
            
            if dlen > 0 then                
                for i = 1, dlen do
                    SceneEvent.setBan(monster.scene_id)
                    local xy = Map.drop_item(monster.scene_id, monster.x, monster.y, dlen)
                    if xy then
                      for i = 1, dlen do
                        ObjItem.enterScene(dtb[i], monster.scene_id, xy[i * 2 - 1], xy[i * 2])
                      end
                    else
                      for i = 1, dlen do
                        ObjItem.enterScene(dtb[i], monster.scene_id, monster.x,  monster.y)
                      end
                    end
                end
            end
        end
    elseif conf.drop_type == DROP_TYPE3 then	--DROP_TYPE3 = 3	-- 所有人共享掉落
        onMonsterDropLogic(nil, monster, conf)
    elseif conf.drop_type == DROP_TYPE4 then    --DROP_TYPE4 = 4	-- 所有攻击过的人独立掉落
        if monster.enemy_list then
            for human_uuid in pairs(monster.enemy_list) do
                local human = ObjHuman.onlineUuid[human_uuid]
                if human and Obj.canSeeEachOther2(human, monster) then
                    onMonsterDropLogic(human, monster, conf)
                end
            end
        end
    elseif conf.drop_type == DROP_TYPE5 then
        local human = onlineUuid[monster.firstHitUuid]
        if not human then
            return
        end
        local team = human.db.teamUuid
        local teamLen = human.db.teamUuidLen
        if teamLen == 0 then
            -- 个人
            onMonsterDropLogic(human, monster, conf, nil, true, conf.drop_type)
        else
            -- 组队
            local humanInCopy = Copy.isCopyBySceneID(human.scene_id)
            for i=0, teamLen - 1 do
                local member = onlineUuid[RoleFFI.ffi_string(team[i].uuid)]
                if member and (Obj.canSeeEachOther2(human, member) or (human.scene_id == member.scene_id and humanInCopy)) then
                    onMonsterDropLogic(member, monster, conf, nil, true, conf.drop_type)
                end
            end
        end
    end
end

local  retlist = {}
function getMonsterDropGrids(human, monsterID)
    local conf = ExcelMonsterConf.monster[monsterID]
    if conf == nil then
        return 0, nil
    end
    local len = 0
    --{物品ID, 绑定概率, 数量, 抽选机率, {排除品质}, minLv, maxLv}
    for _ , v in ipairs(conf.drop) do
        if not v[6] or human and human.db.lv >= v[6] and human.db.lv <= v[7] then
            local r = math.random(1, 10000)
            if v[4] >= r then
                len = len + 1
                retlist[len] = {}
                Grid.create(retlist[len], v[1], v[3], v[2] >= math.random(1, 100), v[5])
            end
        end
    end
    --世界掉落
    local world_drop_conf = WorldDropConf[conf.world_drop_id]
    if conf.world_drop_id < 1 or not world_drop_conf then
        return len, retlist
    end

    for _ , v in ipairs(world_drop_conf) do
        local r = math.random(1, 100)
        if v.group_rate >= r  then
            local cnt = math.random(v.group_start, v.group_end)
            if cnt > 0 and cnt <= #v.items then
                local index = getIndex(v.items, cnt, human)
                for i = 1 , cnt do
                    if index[i] then
                        local c = v.items[index[i] ]
                        len = len + 1
                        retlist[len] = {}
                        Grid.create(retlist[len], c[1], c[3], c[2] >= math.random(1, 100), c[5])
                    end
                end
            end
        end
    end
    return len, retlist
end

local tb = {}
function onMonsterDropLogic(human, monster, conf, noEnterScene, sendArea, dropType)
    local owner_uuid = nil
    local scene_id = nil
    local copyAdd = 1
    if not human then
        scene_id = monster.scene_id
    else
        if not sendArea then
            if not checkCanDrop(human, monster, conf) then
                return
            end
        end
        scene_id = human.scene_id
        owner_uuid = human._id
        copyAdd = Copy.getXunbaoAddDropRate(human)
    end

    local itemList = nil --返回物品列表
    if noEnterScene then
        itemList = {}
    end

    local src_x = monster.x
    local src_y = monster.y
    local hx = human and human.x or monster.x
    local hy = human and human.y or monster.y
    local nMonsterType = ExcelMonster.monster[monster.monster_id].monster_type
    local dropYB = PfLogic.isAgentTencent()
    --{物品ID, 绑定概率, 数量, 抽选机率, {排除品质}, minLv, maxLv}
    local len = 0
    for _ , v in ipairs(conf.drop) do
        if not v[6] or human and human.db.lv >= v[6] and human.db.lv <= v[7] then
            local r = math.random(1, 10000)
            if v[4] >= r then
                local tmp = v[3]
                if human and v[1] == ItemDefine.ITEM_ID_YINLIANG then
                    tmp= tmp *(1 + MonthCardLogic.getMonthMoreYinliang(human))
                end
                if dropYB or v[1] ~= ItemDefine.ITEM_ID_DROP_YUANBAO then  
                  tmp = tmp*copyAdd
                  local item = ObjItem.create(v[1], owner_uuid, v[2] >= math.random(1, 100), tmp, src_x, src_y, v[5], nMonsterType, sendArea, dropType)
                  if not noEnterScene then
                      len = len + 1
                      tb[len] = item
                  else
                      itemList[#itemList + 1] = item
                  end
                end
            end
        end
    end

    if 0 < len then
        SceneEvent.setBan(monster.scene_id)
        local xy = Map.drop_item(monster.scene_id, monster.x, monster.y, len, hx, hy)
        if not tb then
            assert()
        end
        for i = 1, len do
            for j = i + 1, len do
                if tb[i].item_id < tb[j].item_id then
                    tb[i], tb[j] = tb[j], tb[i]
                end
            end
        end
        if xy then
            for i = 1, len do
                ObjItem.enterScene(tb[i], monster.scene_id, xy[i * 2 - 1], xy[i * 2])
            end
        else
            for i = 1, len do
                ObjItem.enterScene(tb[i], monster.scene_id, monster.x,  monster.y)
            end
        end
    end
    --世界掉落
    local world_drop_conf = WorldDropConf[conf.world_drop_id]
    if conf.world_drop_id < 1 or not world_drop_conf then
        return itemList
    end
    
    len = 0
    for _ , v in ipairs(world_drop_conf) do
        local r = math.random(1, 100)
        if v.group_rate >= r  then
            local cnt = math.random(v.group_start, v.group_end)
            if cnt > 0 and cnt <= #v.items then
                local index = getIndex(v.items, cnt, human)
                for i = 1 , cnt do
                    if index[i] then
                        local c = v.items[index[i] ]
                        local tmp = c[3]
                        if human and c[1] == ItemDefine.ITEM_ID_YINLIANG then
                            tmp= tmp *( 1 + MonthCardLogic.getMonthMoreYinliang(human))
                        end
                        tmp = tmp * copyAdd
                        if ItemDefine.is_spobj[c[1]] then
                            if dropYB or c[1] ~= ItemDefine.ITEM_ID_YUANBAO then
                              local item = ObjItem.create(c[1], owner_uuid, c[2] >= math.random(1, 100), tmp, src_x, src_y, c[5], nMonsterType, sendArea, dropType)
                              if not noEnterScene then --货币类的，允许掉落在障碍处，（自动拾取）
                                  local x , y = getPos(monster, math.random(1, DROP_RADIUS))
                                  ObjItem.enterScene(item, scene_id, x, y)
                              else
                                  itemList[#itemList + 1] = item
                              end
                            end
                        else
                            if dropYB or c[1] ~= ItemDefine.ITEM_ID_DROP_YUANBAO then
                              for i = 1, tmp do
                                  local item = ObjItem.create(c[1], owner_uuid, c[2] >= math.random(1, 100), 1, src_x, src_y, c[5], nMonsterType, sendArea, dropType)
                                  if not noEnterScene then
                                    len = len + 1
                                    tb[len] = item
                                  else
                                    itemList[#itemList + 1] = item
                                  end
                              end
                            end
                        end
                    end
                end
            end
        end
    end
    if 0 < len then
        SceneEvent.setBan(monster.scene_id)
        local xy = Map.drop_item(monster.scene_id, monster.x, monster.y, len, hx, hy)
        if xy then
            for i = 1, len do
                ObjItem.enterScene(tb[i], monster.scene_id, xy[i * 2 - 1], xy[i * 2])
            end
        else
            for i = 1, len do
                ObjItem.enterScene(tb[i], monster.scene_id, monster.x,  monster.y)
            end
        end
    end
    return itemList
end


function onCollectFinish(collect, human)
    local conf = ObjCollect.getConfig(collect)
    if not conf then
        return
    end

    if collect.dropMonsterID and collect.dropMonsterID > 0 then
        local cfMonster = ExcelMonsterConf.monster[collect.dropMonsterID]
        if cfMonster then
            conf.drop_type = cfMonster.ghost_drop_type
            conf.world_drop_id = cfMonster.ghost_world_drop_id
        end
    end

    local onlineUuid = ObjHuman.onlineUuid

    if conf.drop_type == DROP_COLLECT_TYPE1 then -- 采集者独享
        onCollectDropLogic(human, collect, conf)
    elseif conf.drop_type == DROP_COLLECT_TYPE2 then -- 采集者和队友独立掉落
        local team = human.db.teamUuid
        local teamLen = human.db.teamUuidLen
        if teamLen <= 0 then
            -- 个人
            onCollectDropLogic(human, collect, conf)
        else
            -- 组队
            for i=0, teamLen - 1 do
                local member = onlineUuid[RoleFFI.ffi_string(team[i])]
                if member and Obj.canSeeEachOther2(human, member) then
                    onCollectDropLogic(member, collect, conf)
                end
            end
        end
    elseif conf.drop_type == DROP_COLLECT_TYPE3 then -- 采集者和队友平均掉落
        local team = human.db.teamUuid
        local teamLen = human.db.teamUuidLen
        if teamLen <= 0  then
            onCollectDropLogic(human, collect, conf)
        else
            --　check team rate
            checkTeamRate(team)

            local tmpTeam = getTeam(team, collect, teamLen)

            if #tmpTeam == 0 then
                return
            end

            local itemList = onCollectDropLogic(nil, collect, conf, true)
            local CNT = math.floor(#itemList / #tmpTeam)
            local member = nil
            local scene_id = collect.scene_id

            --平均分
            for _, m in ipairs(tmpTeam) do
                for i=1, CNT do
                    local r = math.random(1, #itemList)
                    local item = table.remove(itemList, r)
                    member = onlineUuid[m.uuid]
                    if member and checkCanDrop(member, collect, conf) then
                        local x , y = getPos(collect, math.random(1, DROP_RADIUS))
                        item.owner_uuid = member._id
                        ObjItem.enterScene(item, scene_id, x, y)
                    else
                        item.scene_id = nil
                        ObjItem.destroy(item)
                    end
                end
            end --end for

            local max_cnt = 0
            for _, v in ipairs(tmpTeam) do
                member = onlineUuid[v.uuid]
                if member then
                    max_cnt = max_cnt + math.ceil(member.teamPickWeight)
                end
            end

            -- 抽选
            for _, item in ipairs(itemList) do

                local destoryItem = true
                local r = math.random(1, max_cnt)
                local sum = 0

                for _, v in ipairs(tmpTeam) do
                    member = onlineUuid[v.uuid]
                    if member then
                        sum = sum + member.teamPickWeight
                        if sum >= r then
                            if checkCanDrop(member, collect, conf) then
                                member.teamPickWeight = math.ceil(member.teamPickWeight * TEAM_PRICK_WEIGHT_RATE)
                                local x , y = getPos(collect, math.random(1, DROP_RADIUS))
                                item.owner_uuid = member._id
                                ObjItem.enterScene(item, scene_id, x, y)
                                destoryItem = false
                            end
                            break
                        end
                    end
                end

                if destoryItem then
                    item.scene_id = nil
                    ObjItem.destroy(item)
                end

            end --end for

        end
    end
end


local tbTemp = {}
function onCollectDropLogic(human, collect, conf, noEnterScene)
    local owner_uuid = nil
    local scene_id = nil
    if not human then
        scene_id = collect.scene_id
    else
        scene_id = human.scene_id
        owner_uuid = human._id
    end

    local itemList = nil --返回物品列表
    if noEnterScene then
        itemList = {}
    end

    local src_x = collect.x
    local src_y = collect.y
    local hx = human and human.x or collect.x
    local hy = human and human.y or collect.y

    --{物品ID, 绑定概率, 数量, 抽选机率, {排除品质}}
    local len = 0
    for _ , v in ipairs(conf.drop) do
        if not v[6] or human and human.db.lv >= v[6] and human.db.lv <= v[7] then
            local r = math.random(1, 10000)
            if v[4] >= r then
                local item = ObjItem.create(v[1], owner_uuid, v[2] >= math.random(1, 100), v[3], src_x, src_y, v[5])
                if not noEnterScene then
                    len = len + 1
                    tbTemp[len] = item
                else
                    itemList[#itemList + 1] = item
                end
            end
        end
    end
    if 0 < len then
        SceneEvent.setBan(collect.scene_id)
        local xy = Map.drop_item(collect.scene_id, collect.x, collect.y, len, hx, hy)
        if not tbTemp then
            assert()
        end
        for i = 1, len do
            for j = i + 1, len do
                if tbTemp[i].item_id < tbTemp[j].item_id then
                    tbTemp[i], tbTemp[j] = tbTemp[j], tbTemp[i]
                end
            end
        end
        for i = 1, len do
            ObjItem.enterScene(tbTemp[i], collect.scene_id, xy[i * 2 - 1], xy[i * 2])
        end
    end

    --世界掉落
    local world_drop_conf = WorldDropConf[conf.world_drop_id]
    if conf.world_drop_id < 1 or not world_drop_conf then
        return itemList
    end

    for _ , v in ipairs(world_drop_conf) do
        local r = math.random(1, 100)
        if v.group_rate >= r  then
            local cnt = math.random(v.group_start, v.group_end)
            if cnt > 0 and cnt <= #v.items then
              local index = getIndex(v.items, cnt, human)
              for i = 1 , cnt do
                if index[i] then
                  local c = v.items[index[i] ]
                  local item = ObjItem.create(c[1], owner_uuid, c[2] >= math.random(1, 100), c[3], src_x, src_y, c[5])
                  local x , y = getPos(collect, math.random(1, DROP_RADIUS))
                  if not noEnterScene then
                    ObjItem.enterScene(item, scene_id, x, y)
                  else
                    itemList[#itemList + 1] = item
                  end
                end
              end
            end
        end
    end
    return itemList
end

function checkCanDrop(human, monster, conf)

    --[[	local checkCanSee = true
    local humanInCopy = Copy.isCopyBySceneID(human.scene_id)
    if conf.drop_type == DROP_TYPE1 and humanInCopy then
    checkCanSee = false
    end

    --判断同场景
    if checkCanSee and not Obj.canSeeEachOther2(human, monster) then
    return
    end
    --]]
    --判断掉落等级限制
    if conf.drop_level_control ~= 0 and human.db.lv - conf.lv >= conf.drop_level_control then
        return
    end

    return true
end

function putItem2Scene(human, grid)
    local item = ObjItem.create(grid.id, nil, grid.bind, grid.cnt, human.x, human.y, nil, Obj.TYPE_HUMAN)
    local tmpGrid = {}
    Grid.copy(tmpGrid, grid, true)
    item.grid = tmpGrid
    item.dropFromHuman = human._id
    local x , y = getPos(human, math.random(1, DROP_RADIUS_HUMAN))
    ObjItem.enterScene(item, human.scene_id, x, y)
end

function getPos(human, r)
    local hudu =  math.random() * 2 * math.pi
    local x = human.x + math.cos(hudu) * r
    local y = human.y + math.sin(hudu) * r
    return Map.getFixXY(human.scene_id, x, y)
end

function getIndex(confs, cnt, human)
    local index = {}
    index[0] = cnt
    for i = 1 , cnt do
        index[i] = RandomLogic.getRandom4(confs, index, 4, 6, 7, human)
    end
    return index
end

function isNear(a, b)
    local dx = a.x - b.x
    local dy = a.y - b.y
    return -PICK_RADIUS < dx and dx < PICK_RADIUS and -PICK_RADIUS < dy and dy < PICK_RADIUS
end
